;(function () {
    // importCss();
    var _global;
    var environment = 'pc';//pc,android,stb
    var defaults = {
        fullMode: false//全屏模式
    };
    var PLAY_STATUS = 1, STOP_STATUS = 0, PAUSE_STATUS = 2;
    var SPlayer = function (selector) {
        this._init(selector);
    };
    SPlayer.prototype = {
        play: function () {
            switch (environment) {
                case 'pc':
                    document.onsystemevent({which:13003});
                    break;
                case 'stb':
                    break;
                case 'android':
                    document.onsystemevent({which:13003});
                    break;
                default:
                    break;
            }
            this.status = PLAY_STATUS;
        },
        //停止播放
        stop: function () {
            switch (environment) {
                case 'pc':
                    break;
                case 'stb':
                    break;
                case 'android':
                    break;
                default:
                    break;
            }
            this.status = STOP_STATUS;
        },
        //恢复播放
        resume: function () {
            switch (environment) {
                case 'pc':
                    break;
                case 'stb':
                    break;
                case 'android':
                    break;
                default:
                    break;
            }
        },
        //暂停播放
        pause: function () {
            switch (environment) {
                case 'pc':
                    break;
                case 'stb':
                    break;
                case 'android':
                    break;
                default:
                    break;
            }
            this.status = PAUSE_STATUS;
        },
        //销毁实例
        destroy: function () {
            switch (environment) {
                case 'pc':
                    break;
                case 'stb':
                    break;
                case 'android':
                    break;
                default:
                    break;
            }
            this.status = STOP_STATUS;
        },
        //加声音
        volUp: function (volNum) {

        },
        //减声音
        volDown: function (volNum) {
        },
        volChange: function (volNum) {
            switch (environment) {
                case 'pc':
                    break;
                case 'stb':
                    break;
                case 'android':
                    break;
                default:
                    break;
            }
        },
        //获取播放器实例
        getPlayerInstance: function () {
        },
        //获取播放器状态
        getPlayerStatus: function () {
            return this.status;
        },
        _init: function (selector) {
            checkEnv();
            this._event();
            if (selector && selector.indexOf('#')) {
                this.elemNodes = document.getElementById(selector.substring(1));
            } else if (selector) {
                this.elemNodes = document.querySelector(selector);
            } else {
                this.elemNodes = document.getElementsByTagName('video')[0];
            }

            var playerHeight = this.elemNodes.offsetHeight;
            var playerWidth = this.elemNodes.offsetWidth;
            var playerTop = this.elemNodes.offsetTop;
            var playerLeft = this.elemNodes.offsetLeft;

            this.mediaSource = this.elemNodes.src;
            environment='stb';
            switch (environment) {
                case 'pc':
                    break;
                case 'stb':
                    this.PlayerInstance = new MediaPlayer();
                    if(GlobalVarManager.getItemValue("PLAYER_INSTANCE_ID")){
                        this.playerID = GlobalVarManager.getItemValue("PLAYER_INSTANCE_ID");
                    }else{
                        this.playerID = this.PlayerInstance.getPlayerInstanceID();
                        GlobalVarManager.setItemValue("PLAYER_INSTANCE_ID",this.playerID);
                    }

                    this.PlayerInstance.enableTrickMode(true);
                    this.PlayerInstance.bindPlayerInstance(this.playerID);
                    this.PlayerInstance.setMediaSource(this.mediaSource);
                    this.PlayerInstance.play();
                    this.PlayerInstance.setVideoDisplayMode(0);
                    this.PlayerInstance.refresh();

                    var mediaObj = document.createElement("object");
                    mediaObj.setAttribute("type", "application/NDS-JS-EXT-videoWindow");
                    mediaObj.setAttribute("width", playerWidth);
                    mediaObj.setAttribute("height", playerHeight);
                    mediaObj.setAttribute("id","videoPluginId");
                    this.elemNodes.parentNode.replaceChild(mediaObj,this.elemNodes);

                    var videoPluginObj = document.getElementById("videoPluginId");
                    ServiceSelection.tune(this.mediaSource,2);
                    videoPluginObj.setOutputVideoWindow(playerLeft,playerTop,playerWidth,playerHeight);
                    videoPluginObj.enableVideoWindow(true);
                    SysUtil.setMute(false,true);

                    break;
                case 'android':
                    break;
                default:
                    break;
            }
        },
        _event: function () {
            var _this = this;
            document.onkeydown = function (keyEvent) {
                keyEvent = keyEvent ? keyEvent : window.event;
                keyEvent.which = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
                if (keyEvent.which === 0x0447) {
                    //音量＋键
                    _this.volUp();
                } else if (keyEvent.which === 0x0448) {
                    //音量－键
                    _this.volDown();
                } else if (keyEvent.which === 0x0013) {
                    //暂停
                    _this.pause();
                } else if (keyEvent.which === 1045 || keyEvent.which === 0x019F) {
                    //播放
                    if (status === '2') {
                        _this.resume();
                    } else if (status === '0') {
                        _this.play();
                    }
                    this.resume();
                } else if (keyEvent.which === 0x000D) {
                    /*  确定  */
                    if (status === '1') {
                        _this.pause();
                    } else if (status === '2') {
                        _this.resume();
                    } else if (status === '0') {
                        _this.play();
                    }
                } else if (keyEvent.which === 113 || keyEvent.which === 0x0008) {//返回
                    vedioDestroyByNewVersion();
                    history.go(-1);
                }
            };

            document.onsystemevent = function (event) {
                var keyCode = event.which;
                switch (keyCode) {
                    case 13003://开始播放成功！
                        _this.status = PLAY_STATUS;
                        // document.write("播放成功");
                        break;
                    case 13004://开始播放失败！
                        _this.status = STOP_STATUS;
                        // document.write("播放失败");
                        break;
                    case 13005://步长设置成功!
                        break;
                    case 13006://步长设置失败!
                        break;
                    case 13007://设置播放时间点成功!
                        break;
                    case 13008://设置播放时间点失败!
                        break;
                    case 13009://暂停播放成功!
                        _this.status = PAUSE_STATUS;
                        break;
                    case 13010://暂停播放失败!
                        break;
                    case 13011://恢复播放成功!
                        _this.status = PLAY_STATUS;
                        break;
                    case 13012://恢复播放失败!
                        break;
                    case 13013://停止播放成功!
                        _this.status = STOP_STATUS;
                        break;
                    case 13014://停止播放失败!
                        break;
                    case 13015://当前媒体已经播放完毕
                        _this.status = STOP_STATUS;
                        break;
                }
            }
        }
    };

    //中间件机顶盒按键事件
    function stbKeyEvent() {

    }

    //安卓盒子按键事件
    function androidKeyEvent() {

    }

    //PC端按键事件
    function pcKeyEvent() {

    }

    function checkEnv() {
        if (typeof MediaPlayer !== 'undefined') {
            environment = 'stb'
        } else if (typeof nativeMethod !== 'undefined') {
            environment = 'android'
        } else {
            environment = 'pc'
        }
    }

    function importCss() {
        var head = document.getElementsByTagName("head");
        var css = document.createElement("link");
        css.href = '../css/plug-in/splayer.css';
        css.rel = "stylesheet";
        css.type = "text/css";
        head[0].appendChild(css);
    }

    function extend(target, options, deep) {
        for (var key in options) {
            if (options.hasOwnProperty(key)) {
                var copy = options[key];
                if (deep && copy instanceof Array) {
                    target[key] = extend(target[key], copy, deep);
                } else if (deep && copy instanceof Object) {
                    target[key] = extend(target[key], copy, deep);
                } else {
                    target[key] = options[key];
                }
            }
        }
        return target;
    }

    _global = (function () {
        return this || (0, eval)('this');
    }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = SPlayer;
    } else if (typeof define === "function" && define.amd) {
        define(function () {
            return SPlayer;
        });
    } else {
        !('SPlayer' in _global) && (_global.SPlayer = SPlayer);
    }
})();